package pso.gui;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;

import org.jdesktop.swingx.JXTable;

import pso.gui.icons.IconLoader;

import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;

import evolution.data.Problem;

public class MCPSOFrame extends JFrame {

	private static final long serialVersionUID = -1559294430884709117L;
	private static final String ROW_SPECS = "pref, 3dlu, pref, 3dlu, pref, 3dlu, pref, 3dlu, pref, 3dlu, pref, 3dlu, pref, 3dlu, pref, 3dlu, 100dlu:grow, 3dlu, pref, 3dlu, pref, 3dlu, pref";
	private static final String COL_SPECS = "pref:grow, 3dlu, 50dlu, 50dlu, pref:grow, 3dlu, 50dlu";
	private static MCPSOFrame instance;

	private JComboBox<Problem> problemComboBox = new JComboBox<Problem>();
	private JSpinner problemDimensionsSpinner = new JSpinner();
	private JSpinner swarmSizeSpinner = new JSpinner();
	private JSpinner beginInertiaSpinner = new JSpinner();
	private JSpinner endInertiaSpinner = new JSpinner();
	private JSpinner inertiaSpinner = new JSpinner();
	private JSpinner iterationsSpinner = new JSpinner();
	private JSpinner epochLengthSpinner = new JSpinner();
	private JSpinner c1FactorSpinner = new JSpinner();
	private JSpinner c2FactorSpinner = new JSpinner();
	private JSpinner c3FactorSpinner = new JSpinner();
	private JXTable slavesTable = new JXTable();
	private JButton okButton = new JButton();
	private JButton addSlaveButton = new JButton();
	private JButton removeSlaveButton = new JButton();

	public static MCPSOFrame getInstance() {
		if (instance == null) {
			instance = new MCPSOFrame();
		}
		return instance;
	}

	private MCPSOFrame() {
		setTitle("MCPSO");
		initGui();
		setIconImage(IconLoader.getIcon("main64.png"));
	}

	public JComboBox<Problem> getProblemComboBox() {
		return problemComboBox;
	}

	public JSpinner getProblemDimensionsSpinner() {
		return problemDimensionsSpinner;
	}

	public JSpinner getSwarmSizeSpinner() {
		return swarmSizeSpinner;
	}

	public JSpinner getBeginInertiaSpinner() {
		return beginInertiaSpinner;
	}

	public JSpinner getEndIntertiaSpinner() {
		return endInertiaSpinner;
	}

	public JSpinner getInertiaSpinner() {
		return inertiaSpinner;
	}

	public JSpinner getIterationsSpinner() {
		return iterationsSpinner;
	}

	public JSpinner getEpochLengthSpinner() {
		return epochLengthSpinner;
	}

	public JXTable getSlavesTable() {
		return slavesTable;
	}

	public JButton getOkButton() {
		return okButton;
	}

	public JButton getAddSlaveButton() {
		return addSlaveButton;
	}

	public JButton getRemoveSlaveButton() {
		return removeSlaveButton;
	}

	public JSpinner getC1FactorSpinner() {
		return c1FactorSpinner;
	}

	public JSpinner getC2FactorSpinner() {
		return c2FactorSpinner;
	}

	public JSpinner getC3FactorSpinner() {
		return c3FactorSpinner;
	}

	private void initGui() {
		FormLayout layout = new FormLayout(COL_SPECS, ROW_SPECS);
		PanelBuilder builder = new PanelBuilder(layout);

		builder.addSeparator("Problem optymalizacyjny", CC.xyw(1, 1, 7));
		builder.add(problemComboBox, CC.xyw(1, 3, 3));
		builder.addLabel("Liczba wymiarów", CC.xy(5, 3));
		builder.add(problemDimensionsSpinner, CC.xy(7, 3));

		builder.addSeparator("Parametry stada nadrzędnego", CC.xyw(1, 5, 7));

		builder.addLabel("Rozmiar stada", CC.xy(1, 7));
		builder.add(swarmSizeSpinner, CC.xy(3, 7));
		builder.addLabel("Iteracje", CC.xy(5, 7));
		builder.add(iterationsSpinner, CC.xy(7, 7));

		builder.addLabel("Epoka", CC.xy(1, 9));
		builder.add(epochLengthSpinner, CC.xy(3, 9));

		builder.addLabel("Współczynnik C1", CC.xy(1, 11));
		builder.add(c1FactorSpinner, CC.xy(3, 11));
		builder.addLabel("Współczynnik C2", CC.xy(5, 11));
		builder.add(c2FactorSpinner, CC.xy(7, 11));

		builder.addLabel("Współczynnik C3", CC.xy(1, 13));
		builder.add(c3FactorSpinner, CC.xy(3, 13));
		builder.addLabel("Waga inercji", CC.xy(5, 13));
		builder.add(inertiaSpinner, CC.xy(7, 13));

		builder.addSeparator("Stada podrzędne", CC.xyw(1, 15, 7));

		// slavesTable.setColumnControlVisible(true);

		builder.add(new JScrollPane(slavesTable),
				CC.xyw(1, 17, 7, "fill, fill"));

		JPanel buttonBar = new ButtonBarBuilder().addButton(addSlaveButton,
				removeSlaveButton).build();

		builder.add(buttonBar, CC.xyw(1, 19, 7, "right, default"));

		builder.addSeparator("", CC.xyw(1, 21, 7));
		builder.add(new ButtonBarBuilder().addButton(okButton).build(),
				CC.xyw(1, 23, 7));

		builder.setDefaultDialogBorder();

		getContentPane().add(builder.getPanel());
	}
}
